
/**************************************************************************************/
/*     art-buf.cpp       Functions for writing chaotix art to buffers & vice versa    */
/*                       By: Josh Fiorio                                              */
/**************************************************************************************/

#include "art-buf.h"

extern int artMag;       /*         functions use this variable for the magnification of art        */       
extern long pcolor;      /* default transparent color, should be declared in another linked source  */

void ArtToBuf(art &it, palette &pal, FrameBuffer &buf)
{
    buf.Clear();
    int pixon=0;
   
	for (int ylp = 0; ylp <= it.nolines; ylp++)
	{
        for (int xlp = it.lines[ylp].x1; xlp <= it.lines[ylp].x2 - 1; xlp++)
		{
			for (int yp=0; yp<artMag; yp++)
			{
				for (int xp=0; xp<artMag; xp++)
				{
					buf.SetPixel (((abs(it.x1) + it.lines[ylp].x1 + pixon)*artMag)+xp, ((abs(it.y1) + it.lines[ylp].Y)*artMag)+yp, c32to256(pal.colors[it.lines[ylp].pixels[pixon]]));
				}	//end for
			}	//end for
			pixon = pixon + 1;
        }	//end for
		
		//back to first pix
        pixon = 0;
    }	//end for
}	//end of func.

void BufToArt(art &it, palette &pal, FrameBuffer &buf)
{
	//erase art
	memset(&it.lines, 0, sizeof(it.lines));
 
	//declare vars
    int xmost=0;
    int ymost=0;
	int xlp=0;
	int ylp=0;

	//get new w&h
    for (ylp=0; ylp <= buf.height; ylp++)
	{
        for (xlp=0; xlp <= buf.width; xlp++)
		{
            if (buf.GetPixel(xlp*artMag, ylp*artMag) != pcolor && buf.GetPixel(xlp*artMag, ylp*artMag) != -1)
			{
                if (xlp > xmost) { xmost = xlp; }
				if (ylp > ymost) { ymost = ylp; }
            }	//end if
		}	//end for
	}	//end for

	//set new x and y positions
    it.x1 = it.x2 - xmost;
    it.x2 = it.x2;
    it.y1 = it.y2 - ymost;
    it.y2 = it.y2;
    
	//declare vars
    int lineLen=0;
    int lineOn=0;

	//get pixels
    for (ylp=0; ylp<=ymost + 1; ylp++)
	{
        for (xlp=0; xlp<=xmost + 1; xlp++)
		{
            if (buf.GetPixel(xlp*artMag, ylp*artMag) != pcolor && buf.GetPixel(xlp*artMag, ylp*artMag) != -1)
			{
				it.lines[lineOn].pixels[lineLen] = GetColorPalette(pal, buf.GetPixel(xlp*artMag, ylp*artMag));
                it.lines[lineOn].Y = ylp + (it.y2 - ymost);
                lineLen = lineLen + 1;
			}
            else
			{
                if (lineLen > 0)
				{
					it.lines[lineOn].x2 = xlp + (it.x2 - xmost);
					it.lines[lineOn].x1 = (xlp - lineLen) + (it.x2 - xmost);
					lineOn = lineOn + 1;
					lineLen = 0;
				}	//end if
			}	//end if					
		}	//end for
    }	//end for
    
	//set stuff
    it.nolines = lineOn;
    
}	//end of func

void GridToBuf(FrameBuffer &b1, long color)
{
	//Exit now if artMag is less than 3, so we don't have all of our art covered
	if (artMag < 3) { return; }

	//Horizontal lines
	for (int ylp=0; ylp<=b1.height; ylp+=artMag)
	{
		for (int xlp=0; xlp<=b1.width; xlp++)
			b1.SetPixel(xlp, ylp, color);
		//end for
	}	//end for

	//Vertical lines
	for (int ylp=0; ylp<=b1.height; ylp++)
	{
		for (int xlp=0; xlp<=b1.width; xlp+=artMag)
			b1.SetPixel(xlp, ylp, color);
		//end for
	}	//end for

}	//end of func.
